/*1942 game clone
 * Authors:
 * Abdollah Tabareh
 * Bharat Bhushan Konka
 * Martin Svensson 
 * Richard Zianne
 * Tigran Harutyunyan
 * 
 * 2010
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Game1942
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Enemy1 : EnemyPlane
    {
        private int _countDown = 8; 
        public Enemy1(Game game, ref Texture2D texture)
            : base(game)
        {
            _power = 10;
            _planeHight = 32;
            _planeWith = 32;
            _planeSpeed = 1;
            _position = new Vector2(Util.ScreenWidth/2, Util.ScreenHeight/2);
            _texture = texture;
            

            _isDying = false;
            _isDead = false;

            //_spriteRectangle = new Rectangle(4, 70, _planeWith, _planeHight);
            _spriteBatch =(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            Frames = new List<Rectangle>();
            Rectangle frame = new Rectangle();
            frame.X = 4;
            frame.Y = 70;
            frame.Width = _planeWith;
            frame.Height = _planeHight;
            Frames.Add(frame); //index 0 = down 

            frame.X = _planeWith * 1 + 5;           
            Frames.Add(frame);//index 1 = downRight

            frame.X = _planeWith * 2 + 6;
            Frames.Add(frame);//index 2 = Right

            frame.X = _planeWith * 3 + 7;
            Frames.Add(frame);//index 3 = upRight

            frame.X = _planeWith * 4 + 8;
            Frames.Add(frame);//index 4 = up

            frame.X = _planeWith * 5 + 9;
            Frames.Add(frame);//index 5 = upLeft

            frame.X = _planeWith * 6 +10;
            Frames.Add(frame);//index 6 = Left

            frame.X = _planeWith * 7 + 11;
            Frames.Add(frame);//index 7 = downLeft

            //Explosion
            frame.X = 70;
            frame.Y = 169;
            Frames.Add(frame);//index 8 Explosion 1

            frame.X = 103;
            Frames.Add(frame);//index 9 Explosion 2

            frame.X = 136;
            Frames.Add(frame);//index 10 Explosion 3

            frame.X = 169;
            Frames.Add(frame);//index 11 Explosion 4

            frame.X = 202;
            Frames.Add(frame);//index 12 Explosion 5

            frame.X = 235;
            Frames.Add(frame);//index 13 Explosion 6


            
            _spriteRectangle = Frames.ElementAt(0); 


                        
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {

            if (!_isDead||!_isDying)
            {
                if (_direction == Direction.Down)
                    moveDown(_planeSpeed);
                else if (_direction == Direction.DownRight)
                    moveDownRight(_planeSpeed);
                else if (_direction == Direction.DownLeft)
                    moveDownLeft(_planeSpeed);
                else if (_direction == Direction.Left)
                    moveLeft(_planeSpeed);
                else if (_direction == Direction.Right)
                    moveRight(_planeSpeed);
                else if (_direction == Direction.Up)
                    moveUp(_planeSpeed);
                else if (_direction == Direction.UpLeft)    
                    moveUpLeft(_planeSpeed);
                else if (_direction == Direction.UpRight)  
                    moveUpRight(_planeSpeed);
                
                }
                
                if (_isDying)
                {
                    if (_countDown >= Frames.Count)
                    {
                        _isDead = true;
                        putInPosition(-32, -32);
                        _countDown = 8;
                        

                    }
                    else
                    {
                        _spriteRectangle = Frames.ElementAt(_countDown);
                        _countDown++;
                    }
                    
                }
            

            base.Update(gameTime);
        }
        

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, _position, _spriteRectangle, Color.White);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
        //Move the plane
        //UP
        protected void moveUp(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(4); 
            
        }
        //Down
        protected void moveDown(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(0); 
        }
        //Left
        protected void moveLeft(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(6); 
        }
        //Right
        protected void moveRight(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(2); 
        }
        //upLeft
        protected void moveUpLeft(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(5); 
        }
        //DownLeft
        protected void moveDownLeft(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(7); 
        }
        //UpRight
        protected void moveUpRight(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(3); 
            
        }
        //DownRight
        protected void moveDownRight(float speed)
        {
            _position += _direction * speed;
            _spriteRectangle = Frames.ElementAt(1); 
        }


       
        /*
        public bool CheckCollision(Rectangle rect)
        {
            Rectangle spriterect = new Rectangle((int)_position.X, (int)_position.Y,_planeWith,_planeHight);
            return spriterect.Intersects(rect);
            
        }
        public Rectangle Bounds()
        {
            return new Rectangle((int)_position.X, (int)_position.Y, (int)_planeWith, (int)_planeHight);
        }
        */

    }
}